/*
var UserAdminSelectedRow;
		
function onUserAdminEditorContextMenu(grid, rowIndex, e) {
    e.stopEvent();
    var coords = e.getXY();
    UserAdminContextMenu.rowRecord = grid.store.getAt(rowIndex);
    grid.selModel.selectRow(rowIndex);
    UserAdminSelectedRow=rowIndex;
    UserAdminContextMenu.showAt([coords[0], coords[1]]);
}

UserAdminContextMenu = new Ext.menu.Menu({
    id: 'UserAdminContextMenu',
	items: [
  		{ text: 'Modify this President', handler: modifyPresidentContextMenu },
  		{ text: 'Delete this President', handler: deletePresidentContextMenu },
  		'-',
  		{ text: 'Print this grid', handler: printListingContextMenu }
	]
}); 
*/

var teamAdminWindows = new Array();

MyDesktop.TeamAdmin = Ext.extend(Ext.app.Module, {

    init : function(){
        this.launcher = {
            text: _('Team Administration'),
            iconCls:'teams',
            handler : this.createWindow,
            windowId : 'TeamAdmin',
            scope: this
        }
    },

    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var winname = src.windowId;
        var win = desktop.getWindow(winname);

        if(!win){
			var teamAdmDS = new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({
					url: getMainUrl(),
					method: 'POST'
					}),
					baseParams: {action: 'team_admin'},
				reader: new Ext.data.JsonReader({
						root: 'results',
						id: 'id'
					},[
					{name: 'name',type:'string'},
					{name: 'status',type:'string'},
					{name: 'lang',type:'string'},
					{name: 'update',type:'bool',mapping: 'upd'}
					]),
					sortInfo: {field: 'name',direction:'ASC'}
			});
			
			var teamAdmSM = new Ext.grid.CheckboxSelectionModel(
			);
			
			var teamAdmView = new Ext.grid.GridView({
   		    	getRowClass : function (row, index) { 
			      var cls = ''; 
			      var data = row.data; 
			      switch (data.status) { 
			         case 'INACTIVE' : 
			            cls = 'red-row' 
			            break; 
			      } 
			      return cls; 
			   } 
			});

		    var checkColumn = new Ext.grid.CheckColumn({
		       header: _('Updatable'),
		       dataIndex: 'update',
		       width: 60
		    });
		    
			var teamAdmCM = new Ext.grid.ColumnModel(
				[teamAdmSM,{
					header: _('Team'),
					readOnly: true,
					dataIndex: 'name',
					hidden: false
				},{
					header: _('Status'),
					readOnly: true,
					dataIndex: 'status',
					renderer: translate,
					hidden: false
				},{
					header: _('Language Code'),
					readOnly: true,
					dataIndex: 'lang',
					hidden: false
				}, checkColumn]
			);
			
			teamAdmCM.defaultSortable = true;

			var filters = new Ext.ux.grid.GridFilters({
					menuFilterText: _('Filter'),
					filters:[
					{type: 'string',  dataIndex: 'name'},
					{type: 'list', dataIndex: 'status',	options: [{id: ACTIVE, text: _('Enabled')},{id: INACTIVE, text: _('Disabled')}]
					}],
					local: true,
			});
						
			var teamAdmEd = new Ext.grid.GridPanel({
				id: 'teamAdmGrid',
				loadMask: {msg: _('Loading...')},
				store: teamAdmDS,
				cm: teamAdmCM,
				selModel: teamAdmSM,
				view: teamAdmView,
				plugins: filters,
				plugins:checkColumn,
				tbar: [{
					text: _('Enable/Disable'),
					tooltip: _('Enable/Disable the selected elements'),
					iconCls: 'toggle',
					handler: function() {
							if (teamAdmEd.selModel.getCount()>0) {
								var selModel = teamAdmEd.selModel;
								var selections = selModel.getSelections();
							  	var prez = [];
							  	for(i = 0; i< selModel.getCount(); i++){
									prez.push(selections[i].json.id);
							  	}
							  	var encoded_array = Ext.encode(prez);

							  	var util = new Verbum.Utilities();
								util.request({
									el: teamAdmEd.getEl(),
									msg: _('Saving...'),
						            params: { 
						               action: "team_admin", 
						               ids:  encoded_array,
						               op: 'toggle'
						              }								
								});

								util.on({
									'done': function() {
										teamAdmDS.reload();
									}
								});
							
							} else {
								Ext.Msg.message(_('Please, select an item from the list.'));
							}

						}
				},'-',{
					text: _('Remove'),
					iconCls: 'remove',
					handler: function() {
							if (teamAdmEd.selModel.getCount()>0) {
								Verbum.Util.confirm(_('Sure?'), function() {
											var selModel = teamAdmEd.selModel;
											var selections = selModel.getSelections();
										  	var prez = [];
										  	for(i = 0; i< selModel.getCount(); i++){
												prez.push(selections[i].json.id);
										  	}
										  	var encoded_array = Ext.encode(prez);
											
											var util = new Verbum.Utilities();
											util.request({
												el: teamAdmEd.getEl(),
												msg: _('Saving...'),
									            params: { 
									               action: "team_admin", 
									               ids:  encoded_array,
									               op: 'rm'
									              }											
											});
	
											util.on({
												'done': function() {
													teamAdmDS.reload();
												}
											});
								});	
								
							} else {
								Ext.Msg.message(_('Please, select an item from the list.'));
							}

						}
				},'-',{
					text: _('New Team'),
					iconCls: 'add',
					handler: function() {
						
						if (teamAdminWindows[0]) {
							Ext.destroy(teamAdminWindows[0]);	
						}

						var ed = new Verbum.EditWindow({
							id: 'teamEd',
							autoLoad: getMainUrl('action=team_admin&op=new'),
							url: getMainUrl('action=team_admin&op=save'),
							title: _('New Team'),
				            width: 350,
				            height: 150,
				            iconCls: 'add',	
				            focus: 'name',								
						});
						ed.show();

						ed.on({
							'success': function() {
								teamAdmDS.reload();
							}
						});
						
						teamAdminWindows[0] = ed.current;
					}
				}]	
			});

			checkColumn.on({
				'datachanged': {
					fn: function(el) {
						teamAdmDS.commitChanges();
					}
				}				
			});					

			teamAdmDS.on({
				'update': {
					fn: function(store,record,op) {
						if (Ext.data.Record.COMMIT==op) {
							Verbum.Util.request({
					            params: { 
					               action: "team_admin", 
					               id:  record.id,
					               value: record.get('update'),
					               op: 'up'
					              }											
							});
							
						}
					}
				}	
			});	
			
            win = desktop.createWindow({
				id: winname,
				title: src.text,
				closable:true,
				width:450,
				height:350,
				plain:true,
				layout: 'fit',
				iconCls: 'teams',
				items: teamAdmEd
            });

			win.on({
				'close': {
					fn: function() {
						for (i=0;i<teamAdminWindows.length;i++) {
							Ext.destroy(teamAdminWindows[i]);	
						}
					}
				}
			});

            teamAdmDS.load();

        }
          
        win.show();
    },
	display : function(){
        this.createWindow();
	}
});
